/*
 * poker.cpp
 *
 *  Created on: Nov 8, 2011
 *      Author: administratorforpc
 */
#include "myPPlayer.h"
#include "pokerGame.h"
#include "MAHeaders.h"
#include "Util.h"
#include <MAUI/ListBox.h>
#include <MAUI/Image.h>
#include <madmath.h>
#include <mastdlib.h>
#include <conprint.h>

using namespace MAUtil;
using namespace MAUI;


pokerGame::pokerGame(Screen *previous, User *myUser) : previous(previous), myUser(myUser) {

	for(int x = 0; x < 5; x++){
		checked[x] = false;
	}
	int score = 0;
	turn = false;
	mainLayout = createMainLayout("Deal", "Back");
	this->setMain(mainLayout);
	ListBox* listBox = (ListBox*) mainLayout->getChildren()[0];
	layout = new Layout(0, 0, scrWidth, scrHeight-mainLayout->getChildren()[1]->getHeight(), listBox, 25, 25);
	layout->setMarginX(5);
	layout->setMarginY(5);
	layout->setPaddingLeft(5);
	layout->setPaddingRight(5);
	layout->setDrawBackground(false);

	myPoker = new myPPlayer();

	for(int i = 0; i < 5; i++) {
			myImage[i] = new Image(0, 0, 0, 0, layout, true, true, CARD_DOWN);
	}

	label = createLabel( "Score: ", 32);
	label->setAutoSizeY();
	label->setSkin(gSkin);
	label->setMultiLine(true);
	listBox->add(label);

	for(int j = 0; j < 5; j++){
		setCard(j);
	}
}

pokerGame::~pokerGame() {


}

void pokerGame::keyPressEvent(int keyCode, int nativeCode) {

	if(keyCode == MAK_SOFTRIGHT) {
		previous->show();
	}

	if(keyCode == MAK_SOFTLEFT){

	}

	if(keyCode == MAK_1)
	{
		if(turn == false){
			for(int i = 0; i < 5; i++){
				if(checked[i] == true){
					myPoker->giveCard(i);
				}
			}

			for(int x = 0; x < 5; x++){
				setCard(x);
			}
			turn = !turn;
		}

		else{
			score = myPoker->myHand() + score; // set Highscore
			label->setCaption("Score: " + score);
			myPoker = new myPPlayer();
			for(int j = 0; j < 5; j++){
				setCard(j);
			}
			turn = !turn;
		}
	}
	if(keyCode == MAK_2)
	{
		previous->show();
	}
}

void pokerGame::pointerPressEvent(MAPoint2d point) {
	Point p;
	p.set(point.x, point.y);

	for(int i = 0; i < 5; i++){
		if(myImage[i]->contains(p) && turn == false)
		{
			if(checked[i] == false)
			{
				invertImage(i);
				checked[i] = true;
			}
			else{
				setCard(i);
				checked[i] = false;
			}
		}
	}


	if(softKeys->contains(p)) {
		if(softKeys->getChildren()[0]->contains(p)) {
			keyPressEvent(MAK_SOFTLEFT, 0);
		}
		else if(softKeys->getChildren()[1]->contains(p)) {
			keyPressEvent(MAK_SOFTRIGHT, 0);
		}
	}

}

void pokerGame::pointerReleaseEvent(MAPoint2d point) {

}

void pokerGame::setCard(int num){

	int curSuit;
	int curCard;

	curSuit = myPoker->getSuit(num);
	curCard = myPoker->getCard(num);

	if(curSuit == 0){		// suit = Spades

		if(curCard == 0){
			myImage[num]->setResource(ACE_SPADES);
		}

		else if(curCard == 1){
			myImage[num]->setResource(TWO_SPADES);
		}

		else if(curCard == 2){
			myImage[num]->setResource(THREE_SPADES);
		}

		else if(curCard == 3){
			myImage[num]->setResource(FOUR_SPADES);
		}

		else if(curCard == 4){
			myImage[num]->setResource(FIVE_SPADES);
		}

		else if(curCard == 5){
			myImage[num]->setResource(SIX_SPADES);
		}

		else if(curCard == 6){
			myImage[num]->setResource(SEVEN_SPADES);
		}

		else if(curCard == 7){
			myImage[num]->setResource(EIGHT_SPADES);
		}

		else if(curCard == 8){
			myImage[num]->setResource(NINE_SPADES);
		}

		else if(curCard == 9){
			myImage[num]->setResource(TEN_SPADES);
		}

		else if(curCard == 10){
			myImage[num]->setResource(JACK_SPADES);
		}

		else if(curCard == 11){
			myImage[num]->setResource(QUEEN_SPADES);
		}

		else if(curCard == 12){
			myImage[num]->setResource(KING_SPADES);
		}

		else{
			myImage[num]->setResource(ACE_SPADES);
		}
	}

	else if(curSuit == 1){		// suit = Clubs

		if(curCard == 0){
			myImage[num]->setResource(ACE_CLUBS);
		}

		else if(curCard == 1){
			myImage[num]->setResource(TWO_CLUBS);
		}

		else if(curCard == 2){
			myImage[num]->setResource(THREE_CLUBS);
		}

		else if(curCard == 3){
			myImage[num]->setResource(FOUR_CLUBS);
		}

		else if(curCard == 4){
			myImage[num]->setResource(FIVE_CLUBS);
		}

		else if(curCard == 5){
			myImage[num]->setResource(SIX_CLUBS);
		}

		else if(curCard == 6){
			myImage[num]->setResource(SEVEN_CLUBS);
		}

		else if(curCard == 7){
			myImage[num]->setResource(EIGHT_CLUBS);
		}

		else if(curCard == 8){
			myImage[num]->setResource(NINE_CLUBS);
		}

		else if(curCard == 9){
			myImage[num]->setResource(TEN_CLUBS);
		}

		else if(curCard == 10){
			myImage[num]->setResource(JACK_CLUBS);
		}

		else if(curCard == 11){
			myImage[num]->setResource(QUEEN_CLUBS);
		}

		else if(curCard == 12){
			myImage[num]->setResource(KING_CLUBS);
		}

		else{
			myImage[num]->setResource(ACE_CLUBS);
		}
	}

	else if(curSuit == 2){		// suit = Hearts

		if(curCard == 0){
			myImage[num]->setResource(ACE_HEARTS);
		}

		else if(curCard == 1){
			myImage[num]->setResource(TWO_HEARTS);
		}

		else if(curCard == 2){
			myImage[num]->setResource(THREE_HEARTS);
		}

		else if(curCard == 3){
			myImage[num]->setResource(FOUR_HEARTS);
		}

		else if(curCard == 4){
			myImage[num]->setResource(FIVE_HEARTS);
		}

		else if(curCard == 5){
			myImage[num]->setResource(SIX_HEARTS);
		}

		else if(curCard == 6){
			myImage[num]->setResource(SEVEN_HEARTS);
		}

		else if(curCard == 7){
			myImage[num]->setResource(EIGHT_HEARTS);
		}

		else if(curCard == 8){
			myImage[num]->setResource(NINE_HEARTS);
		}

		else if(curCard == 9){
			myImage[num]->setResource(TEN_HEARTS);
		}

		else if(curCard == 10){
			myImage[num]->setResource(JACK_HEARTS);
		}

		else if(curCard == 11){
			myImage[num]->setResource(QUEEN_HEARTS);
		}

		else if(curCard == 12){
			myImage[num]->setResource(KING_HEARTS);
		}

		else{
			myImage[num]->setResource(ACE_HEARTS);
		}

	}

	else if(curSuit == 3){		// suit = Diamonds

		if(curCard == 0){
			myImage[num]->setResource(ACE_DIAMONDS);
		}

		else if(curCard == 1){
			myImage[num]->setResource(TWO_DIAMONDS);
		}

		else if(curCard == 2){
			myImage[num]->setResource(THREE_DIAMONDS);
		}

		else if(curCard == 3){
			myImage[num]->setResource(FOUR_DIAMONDS);
		}

		else if(curCard == 4){
			myImage[num]->setResource(FIVE_DIAMONDS);
		}

		else if(curCard == 5){
			myImage[num]->setResource(SIX_DIAMONDS);
		}

		else if(curCard == 6){
			myImage[num]->setResource(SEVEN_DIAMONDS);
		}

		else if(curCard == 7){
			myImage[num]->setResource(EIGHT_DIAMONDS);
		}

		else if(curCard == 8){
			myImage[num]->setResource(NINE_DIAMONDS);
		}

		else if(curCard == 9){
			myImage[num]->setResource(TEN_DIAMONDS);
		}

		else if(curCard == 10){
			myImage[num]->setResource(JACK_DIAMONDS);
		}

		else if(curCard == 11){
			myImage[num]->setResource(QUEEN_DIAMONDS);
		}

		else if(curCard == 12){
			myImage[num]->setResource(KING_DIAMONDS);
		}

		else{
			myImage[num]->setResource(ACE_DIAMONDS);
		}
	}

}


void pokerGame::invertImage(int num){
	int curSuit;
	int curCard;

	curSuit = myPoker->getSuit(num);
	curCard = myPoker->getCard(num);

	if(curSuit == 0){		// suit = Spades

		if(curCard == 0){
			myImage[num]->setResource(ACE_SPADES_C);
		}

		else if(curCard == 1){
			myImage[num]->setResource(TWO_SPADES_C);
		}

		else if(curCard == 2){
			myImage[num]->setResource(THREE_SPADES_C);
		}

		else if(curCard == 3){
			myImage[num]->setResource(FOUR_SPADES_C);
		}

		else if(curCard == 4){
			myImage[num]->setResource(FIVE_SPADES_C);
		}

		else if(curCard == 5){
			myImage[num]->setResource(SIX_SPADES_C);
		}

		else if(curCard == 6){
			myImage[num]->setResource(SEVEN_SPADES_C);
		}

		else if(curCard == 7){
			myImage[num]->setResource(EIGHT_SPADES_C);
		}

		else if(curCard == 8){
			myImage[num]->setResource(NINE_SPADES_C);
		}

		else if(curCard == 9){
			myImage[num]->setResource(TEN_SPADES_C);
		}

		else if(curCard == 10){
			myImage[num]->setResource(JACK_SPADES_C);
		}

		else if(curCard == 11){
			myImage[num]->setResource(QUEEN_SPADES_C);
		}

		else if(curCard == 12){
			myImage[num]->setResource(KING_SPADES_C);
		}

		else{
			myImage[num]->setResource(ACE_SPADES_C);
		}
	}

	else if(curSuit == 1){		// suit = Clubs

		if(curCard == 0){
			myImage[num]->setResource(ACE_CLUBS_C);
		}

		else if(curCard == 1){
			myImage[num]->setResource(TWO_CLUBS_C);
		}

		else if(curCard == 2){
			myImage[num]->setResource(THREE_CLUBS_C);
		}

		else if(curCard == 3){
			myImage[num]->setResource(FOUR_CLUBS_C);
		}

		else if(curCard == 4){
			myImage[num]->setResource(FIVE_CLUBS_C);
		}

		else if(curCard == 5){
			myImage[num]->setResource(SIX_CLUBS_C);
		}

		else if(curCard == 6){
			myImage[num]->setResource(SEVEN_CLUBS_C);
		}

		else if(curCard == 7){
			myImage[num]->setResource(EIGHT_CLUBS_C);
		}

		else if(curCard == 8){
			myImage[num]->setResource(NINE_CLUBS_C);
		}

		else if(curCard == 9){
			myImage[num]->setResource(TEN_CLUBS_C);
		}

		else if(curCard == 10){
			myImage[num]->setResource(JACK_CLUBS_C);
		}

		else if(curCard == 11){
			myImage[num]->setResource(QUEEN_CLUBS_C);
		}

		else if(curCard == 12){
			myImage[num]->setResource(KING_CLUBS_C);
		}

		else{
			myImage[num]->setResource(ACE_CLUBS_C);
		}
	}

	else if(curSuit == 2){		// suit = Hearts

		if(curCard == 0){
			myImage[num]->setResource(ACE_HEARTS_C);
		}

		else if(curCard == 1){
			myImage[num]->setResource(TWO_HEARTS_C);
		}

		else if(curCard == 2){
			myImage[num]->setResource(THREE_HEARTS_C);
		}

		else if(curCard == 3){
			myImage[num]->setResource(FOUR_HEARTS_C);
		}

		else if(curCard == 4){
			myImage[num]->setResource(FIVE_HEARTS_C);
		}

		else if(curCard == 5){
			myImage[num]->setResource(SIX_HEARTS_C);
		}

		else if(curCard == 6){
			myImage[num]->setResource(SEVEN_HEARTS_C);
		}

		else if(curCard == 7){
			myImage[num]->setResource(EIGHT_HEARTS_C);
		}

		else if(curCard == 8){
			myImage[num]->setResource(NINE_HEARTS_C);
		}

		else if(curCard == 9){
			myImage[num]->setResource(TEN_HEARTS_C);
		}

		else if(curCard == 10){
			myImage[num]->setResource(JACK_HEARTS_C);
		}

		else if(curCard == 11){
			myImage[num]->setResource(QUEEN_HEARTS_C);
		}

		else if(curCard == 12){
			myImage[num]->setResource(KING_HEARTS_C);
		}

		else{
			myImage[num]->setResource(ACE_HEARTS_C);
		}

	}

	else if(curSuit == 3){		// suit = Diamonds

		if(curCard == 0){
			myImage[num]->setResource(ACE_DIAMONDS_C);
		}

		else if(curCard == 1){
			myImage[num]->setResource(TWO_DIAMONDS_C);
		}

		else if(curCard == 2){
			myImage[num]->setResource(THREE_DIAMONDS_C);
		}

		else if(curCard == 3){
			myImage[num]->setResource(FOUR_DIAMONDS_C);
		}

		else if(curCard == 4){
			myImage[num]->setResource(FIVE_DIAMONDS_C);
		}

		else if(curCard == 5){
			myImage[num]->setResource(SIX_DIAMONDS_C);
		}

		else if(curCard == 6){
			myImage[num]->setResource(SEVEN_DIAMONDS_C);
		}

		else if(curCard == 7){
			myImage[num]->setResource(EIGHT_DIAMONDS_C);
		}

		else if(curCard == 8){
			myImage[num]->setResource(NINE_DIAMONDS_C);
		}

		else if(curCard == 9){
			myImage[num]->setResource(TEN_DIAMONDS_C);
		}

		else if(curCard == 10){
			myImage[num]->setResource(JACK_DIAMONDS_C);
		}

		else if(curCard == 11){
			myImage[num]->setResource(QUEEN_DIAMONDS_C);
		}

		else if(curCard == 12){
			myImage[num]->setResource(KING_DIAMONDS_C);
		}

		else{
			myImage[num]->setResource(ACE_DIAMONDS_C);
		}
	}

}




